package com.zjitc.common;

/**
 * Created by tt on 2018/3/28.
 */
public class ServerResponse<T> {

    private Integer status;
    private String msg;
    private T data;

    public ServerResponse() {
    }

    public ServerResponse(Integer status) {
        this.status = status;
    }

    public ServerResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ServerResponse(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    public ServerResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public boolean success() {
        return this.status.equals(ServerResponseCode.SUCCESS);
    }

    public static <T> ServerResponse<T> createSucess(Object... args) {
        return create(0, args);
    }

    public static <T> ServerResponse<T> createError(Object... args) {
        return create(1, args);
    }

    private static <T> ServerResponse<T> create(Integer status, Object... args) {
        if (args.length > 2) {
            throw new IllegalArgumentException("argument is more than 2:" + args.length);
        }
        if (args.length == 1) {
            if (args[0] instanceof String) {
                return new ServerResponse<T>(status, (String) args[0]);
            } else {
                return new ServerResponse<T>(status, (T) args[0]);
            }
        }
        if (args.length == 2) {
            return new ServerResponse<T>(status, (String) args[0], (T) args[1]);
        }
        return new ServerResponse<T>(status);
    }
}
